package com.yuanjin.main;

import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.yuanjin.R;
import com.yuanjin.activity.ChatArea3;
import com.yuanjin.util.MyAdaper;
import com.yuanjin.util.MyAdaper.FriendsItem;
import com.yuanjin.util.YuanJin_Application;

public class FriendsListFragment extends Fragment
		implements
			OnItemClickListener {
	private Activity mActivity;
	private ListView listview;
	Vector<FriendsItem> list = new Vector<FriendsItem>();
	Vector<FriendsItem> list2 = new Vector<FriendsItem>();

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mActivity = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.friendslist, container, false);

	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		listview = (ListView) view.findViewById(R.id.friendsList);

		dataInit();
		MyAdaper ma = null;
		ma = new MyAdaper(list, mActivity);
		listview.setAdapter(ma);
		// 对listview对象进行监听，当点击子项目时触发
		listview.setOnItemClickListener(this);

	}

	public void dataInit() {
		// YuanJin_Application类中保存了返回的数据的总数receiveData_Sum和数据存储的字符串receiveData
		YuanJin_Application md = (YuanJin_Application) mActivity
				.getApplication();
		if (md.getReceiveData() != null) {
			// 将字符串转换为数组--提取用户数据分别存储
			String[] dataArray = md.getReceiveData().split("###");
			for (int i = 0; i < md.getReceiveData_Sum(); i++) {
				Log.e("数组内容显示", dataArray[i]);

				// 在静态类中无法调用动态类，所以FriendsItem（）类要设置为静态
				FriendsItem friendsItem = new FriendsItem();
				friendsItem.name = dataArray[i];
				// Log.e("postout", "qianmian。。。"+Integer.toString(index));
				friendsItem.picture = new ImageView(mActivity);
				friendsItem.picture.setImageResource(R.drawable.yuanjin);
				// Log.e("postout", "来了。。。"+Integer.toString(index));
				list.addElement(friendsItem);

			}
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Log.e("onclick", "进入点击事件了");
		FriendsItem map = (FriendsItem) listview.getItemAtPosition(position);
		String title = map.name;
		Intent intent = new Intent(mActivity, ChatArea3.class);
		intent.putExtra("friendsname", title);
		startActivity(intent);

		// Toast.makeText(FriendsList.this, title, Toast.LENGTH_LONG).show();
		// Log.e("itemclick", title + "看着里");

	}

}